extends Control
#主菜单脚本
#处理隐藏HUD 按钮点击逻辑 制作名单滚动动画
#按钮主菜单滑动特效
#press to start 背景渐显 bgm
@onready var weather_rain = $WeatherRain
@onready var buttons_container = $ButtonsContainer
@onready var title_texture = $TitleTexture
@onready var press_start_label = $PressStartLabel
@onready var credits_layer = $CreditsLayer
@onready var scroll_text = $CreditsLayer/ScrollText
@onready var btn_back = $CreditsLayer/Btn_Back
@onready var rain_audio: AudioStreamPlayer = $WeatherRain/rainStream
@onready var bg1: TextureRect = $Background1
@onready var bg2: TextureRect = $Background2
@onready var bgm_player: AudioStreamPlayer2D = $BGM_Player
@onready var btn_music = $Btn_Music

const MUSIC_PLAYER_SCENE = preload("res://MusicPlayer/tscn/node_2d.tscn") 
var btn_music_final_pos: Vector2
#制作人员名单滚动的初始位置和结束位置
var text_start_y: float = 0.0
var text_end_y: float = 0.0
#存储按钮和标题位置
var buttons_final_pos: Vector2
var title_final_pos: Vector2
#玩家是否按下按键
var is_waiting_for_input: bool = true
#储存呼吸动画
var blink_tween: Tween
#声音
var audio_tween: Tween

func _ready() -> void:
	if get_tree().paused:
		get_tree().paused = false
	
	VN_SystemUI.hide_hud()
	VN_SystemUI.force_hide_drink_menu()
	VN_SystemUI.btn_handle.visible = false
	
	bg1.visible = true
	bg1.modulate.a = 1.0
	bg2.visible = true
	bg2.modulate.a = 0.0  #新背景完全透明
	
	if bgm_player.stream:
		bgm_player.play()
	
	#制作人员名单是隐藏的
	credits_layer.visible = false
	
	#把标题和按钮藏起来
	buttons_final_pos = buttons_container.position
	title_final_pos = title_texture.position
   
	#记录按钮和标题的最终位置
	buttons_final_pos = buttons_container.position
	title_final_pos = title_texture.position
	
	#按钮标题移出屏幕
	buttons_container.position.y += 300
	buttons_container.modulate.a = 0.0
	title_texture.position.y -= 400
	title_texture.modulate.a = 0.0
	
	btn_music_final_pos = btn_music.position
	#btn_music.position.y += 100 
	btn_music.modulate.a = 0.0
	
	start_blink_animation()
	
	_connect_signals()
	#设置制作人员名单内容
	#开启全局居中
	var text_content = "[center]"
	
	#标题字号大一点
	text_content += "[font_size=128]XJTLUBI SOFT[/font_size]\n\n\n\n"
	
	#正文字号小一点
	text_content += "[font_size=96]"
	
	text_content += "成员名单\n\n"
	text_content += "Abao\n\n"
	text_content += "Kansal-Hagen\n\n"
	text_content += "Lanmao\n\n"
	text_content += "pppig\n\n"
	text_content += "彦澄\n\n\n\n\n\n"
	
	text_content += "[font_size=128]感谢游玩我们的游戏！[/font_size]\n\n"
	text_content += "[font_size=64]2025卡赢杯参赛作品[/font_size]"
	#闭合标签
	text_content += "[/font_size]"
	text_content += "[/center]"
	
	#赋值给RichTextLabel
	scroll_text.text = text_content
	
	#计算滚动距离
	var screen_height = get_viewport_rect().size.y
	text_start_y = screen_height
	
	text_end_y = -scroll_text.size.y - 100
	
	btn_music.pressed.connect(_on_music_pressed)


func _input(event):
	#如果当前不在等待阶段，直接忽略
	if not is_waiting_for_input:
		return
	
	#如果是键盘按下或者鼠标点击
	if (event is InputEventKey and event.pressed) or \
	   (event is InputEventMouseButton and event.pressed):
		
		#触发进入主菜单逻辑
		_on_any_key_pressed()

func _on_any_key_pressed():
	#改变状态，防止重复触发
	is_waiting_for_input = false
	#立刻关闭正在播放的呼吸动画
	if blink_tween:
		blink_tween.kill()
	#播放淡出动画
	var fade_tween = create_tween()
	fade_tween.tween_property(press_start_label, "modulate:a", 0.0, 0.5)
	#动画结束后直接隐藏节点，节省性能
	fade_tween.tween_callback(func(): press_start_label.visible = false)
	#播放UI浮现动画
	start_entrance_animation()
	#播放一个确认音效
	#$SFX_Confirm.play() 
	#播放背景渐变动画
	start_background_change()

func start_background_change():
	var tween = create_tween()
	var duration = 1.5 
	tween.tween_property(bg2, "modulate:a", 1.0, duration)
	print("开始切换背景...")

func start_blink_animation():
	if blink_tween: blink_tween.kill()#防止重复创建
	
	var tween = create_tween()
	tween.set_loops() #无限循环
	tween.set_trans(Tween.TRANS_SINE)
	
	#让透明度在0.2到1.0之间往复变化
	#1秒变淡，1秒变亮
	tween.tween_property(press_start_label, "modulate:a", 0.2, 1.0)
	tween.tween_property(press_start_label, "modulate:a", 1.0, 1.0)


func start_entrance_animation():
	#等待一小会儿让画面加载完
	await get_tree().create_timer(0.1).timeout
	
	var tween = create_tween()
	#并行播放
	tween.set_parallel(true)
	#设置缓动曲线
	tween.set_trans(Tween.TRANS_CUBIC)
	tween.set_ease(Tween.EASE_OUT)
	
	#按钮动画
	#2秒内移动回终点
	tween.tween_property(buttons_container, "position", buttons_final_pos, 2.5)
	#同时淡入(透明度变回1)
	tween.tween_property(buttons_container, "modulate:a", 1.0, 1.5).set_trans(Tween.TRANS_LINEAR)
	
	#标题动画
	#标题稍微慢一点点出现，更有层次感
	tween.tween_property(title_texture, "position", title_final_pos, 2.8)
	tween.tween_property(title_texture, "modulate:a", 1.0, 1.5).set_trans(Tween.TRANS_LINEAR)
	
	tween.tween_property(btn_music, "position", btn_music_final_pos, 2.5)
	tween.tween_property(btn_music, "modulate:a", 1.0, 1.5).set_trans(Tween.TRANS_LINEAR)

func _connect_signals():
	$ButtonsContainer/Btn_Start.pressed.connect(_on_start_pressed)
	$ButtonsContainer/Btn_Settings.pressed.connect(_on_settings_pressed)
	$ButtonsContainer/Btn_Credits.pressed.connect(_on_credits_pressed)
	$ButtonsContainer/Btn_Quit.pressed.connect(_on_quit_pressed)
	btn_back.pressed.connect(_on_credits_back_pressed)

#按钮逻辑
func _on_start_pressed():
	#锁住按钮，防止玩家重复点击
	buttons_container.mouse_filter = Control.MOUSE_FILTER_IGNORE
	#创建音频淡出动画
	var audio_tween = create_tween()
	audio_tween.set_parallel(true) # 让 BGM 和 雨声 同时变小
	#设置淡出时间为0.5秒
	var fade_time = 5.0
	
	#淡出BGM
	if bgm_player.playing:
		audio_tween.tween_property(bgm_player, "volume_db", -40.0, fade_time)
	
	#淡出雨声
	if rain_audio.playing:
		audio_tween.tween_property(rain_audio, "volume_db", -40.0, fade_time)
	
	#启动转场
	SceneLoader.change_scene_with_grid_spread("res://scene/phone_conversation.tscn")

func _on_settings_pressed():
	VN_SystemUI.open_only_settings()

func _on_quit_pressed():
	get_tree().quit()



#制作人员名单逻辑
var credits_tween: Tween

func _on_credits_pressed():
	credits_layer.visible = true
	weather_rain.visible = false
	
	if audio_tween: audio_tween.kill()
	audio_tween = create_tween()
	
	audio_tween.tween_property(rain_audio, "volume_db", -80.0, 1.5)
	#动画结束后真正停止播放
	audio_tween.tween_callback(func(): rain_audio.stop())
	#重置文字位置到屏幕下方
	scroll_text.position.y = text_start_y
	
	#开始滚动动画
	if credits_tween: credits_tween.kill() # 防止重复
	credits_tween = create_tween()
	#计算文字目标位置
	var screen_height = get_viewport_rect().size.y
	var text_height = scroll_text.size.y
	#让文字的最底部停在屏幕的正中间
	#屏幕中心点 - 文字总高度
	var target_center_y = (screen_height / 2.0) - text_height + 150
	
	#设为线性匀速移动，时间设长一点
	credits_tween.tween_property(scroll_text, "position:y", target_center_y, 10.0).set_trans(Tween.TRANS_LINEAR)
	#播放完毕等待3s
	credits_tween.tween_interval(3.0)
	#动画播完后自动返回
	credits_tween.tween_callback(_on_credits_back_pressed)

func _on_credits_back_pressed():
	#停止动画
	if credits_tween: credits_tween.kill()
	#隐藏层
	credits_layer.visible = false
	weather_rain.visible = true
	
	if buttons_container.modulate.a == 0.0:
		start_entrance_animation()
	
	rain_audio.play()
	if audio_tween: audio_tween.kill()
	audio_tween = create_tween()
	rain_audio.volume_db = -80.0
	var target_db = 5.0 
	audio_tween.tween_property(rain_audio, "volume_db", target_db, 1.5)

func _on_music_pressed():
	print("切换到音乐播放器场景...")
	
	var tween = create_tween()
	tween.tween_property(bgm_player, "volume_db", -80.0, 0.5)
	await tween.finished

	get_tree().change_scene_to_file("res://MusicPlayer/tscn/node_2d.tscn")

func _on_music_player_closed():
	print("播放器已关闭，恢复主菜单音乐")
	
	if is_instance_valid(bgm_player):
		bgm_player.stream_paused = false
